from logger import logger
import time


class YuHun:
    dm = object
    log = object
    count = 250

    def __init__(self, dm, count):
        self.dm = dm
        self.log = logger()
        self.log.info("开始刷御魂")
        self.count = count

    def run(self):
        dm = self.dm
        i = self.count
        while i > 0:
            self.ready()
            self.clickReady()
            if self.clickBeforeEnd() == 1:
                i = i - 1
                print("第%d次挑战" % (self.count - i))
            self.clickFailEnd()
            self.clickBreakTeam()
            self.clickEnd()
            self.clickXunShan()
            time.sleep(1)

    # 准备组队或者寻找队伍
    def ready(self):
        dm = self.dm
        i = 0
        is_mian_page = self.isMainPage()
        if is_mian_page == 1:
            self.cancelZhenYuHun()
            while 1:
                if self.clickJoinLastTeam() == 0:
                    # 点击组队
                    if i < 2:
                        i = i + 1
                        time.sleep(3)
                        continue
                    is_mian_page = self.isMainPage()
                    if is_mian_page == 0:
                        return 0
                    self.teamButtonIsExpan()
                    self.log.info('点击底下组队进行')
                    dm.MoveTo(238, 576)
                    dm.LeftClick()
                    time.sleep(3)
                    break
                else:
                    return 1
        if self.isTeamPage() == 1:
            dm.MoveTo(214, 453)
            dm.LeftClick()
            time.sleep(2)
            dm.MoveTo(424, 485)
            dm.WheelDown()
            dm.WheelDown()
            dm.WheelDown()
            dm.WheelDown()
            dm.WheelDown()
            dm.MoveTo(424, 485)
            dm.LeftClick()
            while 1:
                self.log.info("组队页面进行组队")
                dm.MoveTo(440, 553)
                dm.LeftClick()
                time.sleep(2)
                dm.MoveTo(928, 197)
                dm.LeftClick()
                time.sleep(2)
                if self.isTeamPage() == 0:
                    time.sleep(5)
                    return 1
        return 0

    # 是否为组队页面
    def isTeamPage(self):
        dm = self.dm
        ret = dm.FindMultiColor(824, 507, 1024, 607, "f3b25e-000000", "-15|2|f2b15e,-4|-5|3f3527,-10|11|936f41", 1, 1,
                                924, 553)
        if ret[0] > 0:
            return 1
        else:
            return 0

    def isMainPage(self):
        dm = self.dm
        ret = dm.FindMultiColor(1042, 82, 1132, 182, "a92828-000000", "0|5|a92928,2|18|af3f38,-4|11|c59c74,6|10|c39572",
                                1, 1,
                                1099, 102)
        if ret[0] > 0:
            return 1
        else:
            return 0

    # 判断首页菜单是否展开
    def teamButtonIsExpan(self):
        dm = self.dm
        ret = dm.FindMultiColor(156, 515, 304, 615, "c7bdad-000000", "15|5|f8f3e0,23|1|70685b,15|10|f8f3e0", 1, 1, 222,
                                596)
        if ret[0] < 1:
            self.log.info("展开菜单")
            dm.MoveTo(1073, 559)
            dm.LeftClick()
            time.sleep(1)

    # 加入上一次队伍
    def clickJoinLastTeam(self):
        dm = self.dm
        is_fast_join = dm.FindMultiColor(405, 164, 553, 264, "fe906c-000000", "2|7|f06133,9|12|f0d6ad,15|15|cda37a", 1,
                                         1, 458, 185)
        if is_fast_join[0] > 0:
            self.log.info('加入有自动邀请的队伍')
            dm.MoveTo(209, 224)
            dm.LeftClick()
            dm.LeftClick()
            time.sleep(3)
            return 1
        # 没有快捷加入
        is_normal_join = dm.FindMultiColor(311, 165, 491, 267, "e3d0b3-000000",
                                           "-21|-9|f0d7ae,-9|-13|f5e0be,11|1|faf2d8,-6|-4|f0d8af", 1, 1, 962, 148)
        if is_normal_join[0] > 0:
            self.log.info('加入邀请的队伍')
            dm.MoveTo(128, 233)
            dm.LeftClick()
            dm.LeftClick()
            time.sleep(3)
            return 1
        return 0

    # 点击准备按钮
    def clickReady(self):
        dm = self.dm
        ret = dm.FindMultiColor(935, 535, 1135, 635, "221010-000000",
                                "-74|5|daae71,55|35|f1daac,-52|35|cea46c,44|14|ab8252", 1, 1, 1045, 577)
        if ret[0] > 0:
            self.log.info('点击战斗页面的准备')
            dm.MoveTo(1029, 519)
            dm.LeftClick()
            time.sleep(30)

    def clickBeforeEnd(self):
        dm = self.dm
        ret = dm.FindMultiColor(299, 83, 502, 209, "8e1a11-000000", "48|9|a01b11,13|37|cd917d,9|-12|892217",
                                1, 1, 414, 120)
        if ret[0] > 0:
            self.log.info('战斗胜利时结束')
            dm.MoveTo(460, 130)
            dm.LeftClick()
            time.sleep(6)
            self.clickXunShan()
            dm.MoveTo(554, 614)
            dm.LeftClick()
            time.sleep(3)
            return 1
        return 0

    # 结束的时候点击御魂盒子
    def clickEnd(self):
        dm = self.dm
        ret = dm.FindMultiColor(465, 445, 665, 545, "170d0b-000000", "12|-13|ad2f19,41|1|bc8a3c,38|24|0e0101", 1, 1,
                                535, 485)
        if ret[0] > 0:
            self.log.info('点击结束时候大红蛋')
            dm.MoveTo(85, 109)
            dm.LeftClick()
            time.sleep(3)

    def clickFailEnd(self):
        dm = self.dm
        ret = dm.FindMultiColor(304, 66, 504, 166, "595063-000000", "22|0|544d5d,16|-12|b6a38d,22|22|c2af95", 1, 1, 400,
                                115)
        if ret[0] > 0:
            self.log.info('点击失败时候')
            dm.MoveTo(400, 115)
            dm.LeftClick()
            time.sleep(3)

    # 组队的时候退出退伍
    def clickBreakTeam(self):
        dm = self.dm
        ret = dm.FindMultiColor(827, 478, 1027, 578, "2d2822-000000", "15|3|d8a056,9|2|f3b25e", 1, 1, 887, 532)
        unret = dm.FindMultiColor(827, 478, 1027, 578, "b0a9a1-000000", "64|5|b0a9a1,34|8|aaa39b", 1, 1, 889, 534)
        if ret[0] > 0 or unret[0] > 0:
            self.log.info('退出队伍')
            dm.MoveTo(207, 534)
            dm.LeftClick()
            time.sleep(3)
            dm.MoveTo(679, 392)
            dm.LeftClick()
            time.sleep(2)

    # 点击接受悬赏
    def clickXunShan(self):
        dm = self.dm
        ret = dm.FindMultiColor(678, 323, 826, 423, "5cb667-000000", "3|-7|77b97e,15|-14|5ab665,-13|-4|60ba6b", 1, 1,
                                753, 373)
        if ret[0] > 0:
            self.log.info('接受悬赏')
            dm.MoveTo(753, 373)
            dm.LeftClick()
            time.sleep(1)

    # 点掉真大蛇提示
    def cancelZhenYuHun(self):
        dm = self.dm
        ret = dm.FindMultiColor(303, 182, 498, 282, "2c2925-000000", "-8|7|c1bab3,2|13|2d2925,5|11|d9d1cc", 1, 1, 443,
                                212)
        if ret[0] > 0:
            self.log.info('取消真大蛇提示')
            dm.MoveTo(47, 231)
            dm.LeftClick()
            time.sleep(1)
